/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

/**
 * This class is used to draw an image.
 */
public class SceneImage extends SceneLeaf implements ScenicImageObserver {
	private ScenicImage image;
	private Rectangle2D sourceRect;
	
	/**
	 * Creates a SceneImage object with the given image.
	 * 
	 * @param image the image.
	 */
	public SceneImage(ScenicImage image) {
		setImage(image);
	}
	
	/**
	 * Creates a SceneImage object with the given image.
	 * 
	 * @param image the image.
	 * @param sourceRect the part of the image that is displayed.
	 */
	public SceneImage(ScenicImage image, Rectangle2D sourceRect) {
		setImage(image);
		this.sourceRect = sourceRect;
	}
	
	/** 
	 * Gets the image.
	 */
	public ScenicImage getImage() {
		return image;
	}

	/**
	 * Sets the image.
	 */
	public void setImage(ScenicImage image) {
		if(image != null)
			image.deleteObserver(this);
		this.image = (ScenicImage)image;
		if(isVisible() && image != null)
			image.addObserver(this);
		changed();
	}

	/**
	 * Gets the source rectangle. The source rectangle specifies
	 * the part of the image that is displayed. If the rectangle is
	 * null, the entire image is displayed.
	 */
	public Rectangle2D getSourceRect() {
		return sourceRect;
	}

	/**
	 * Sets the source rectangle. The source rectangle specifies
	 * the part of the image that is displayed. If the rectangle is
	 * null, the entire image is displayed.
	 */
	public void setSourceRect(Rectangle2D sourceRect) {
		this.sourceRect = sourceRect;
		changed();
	}

	protected void show() {
		super.show();
		if(image != null) {
			image.addObserver(this);
		}
	}
	
	protected void hide() {
		super.hide();
		if(image != null) {
			image.deleteObserver(this);
		}
	}
	
	void draw(DrawContext context, AffineTransform transform, Rectangle visibleArea) {
		if(image == null || image.getWidth() == 0 || image.getHeight() == 0)
			return;

		context.renderer.color(context.context, new ScenicColor(1.0f, 1.0f, 1.0f, 1.0f));
		context.renderer.setTransform(context.context, transform);
		if(sourceRect != null)
			context.renderer.drawImage(context.context, image.getId(), 
					sourceRect.getX(), sourceRect.getY(), 
					sourceRect.getWidth(), sourceRect.getHeight());
		else
			context.renderer.drawImage(context.context, image.getId(), 
					0, 0, image.getWidth(), image.getHeight());
	}

	Rectangle getBounds(DrawContext context, AffineTransform transform) {
		if(sourceRect != null) {
			return Util.transform(transform, 
					new Rectangle(0, 0, (int)sourceRect.getWidth(), (int)sourceRect.getHeight()));			
		} else {
			return Util.transform(transform, 
					new Rectangle(0, 0, image.getWidth(), image.getHeight()));
		}
	}

	public void update(ScenicImage image) {
		changed(null);
	}	
}
